/**
 * Created by smp on 2017/4/24.
 */
var modelPath=ROOT_PATH+'/majorHazard/activityRecord/';
$(function(){

var contactContractorNames=$("#contactContractorName").textbox("getValue").split(',');
var contactContractors=$("#contactContractor").val().split(',');
var contactNames=[];
for(var j=0;j<contactContractorNames.length;j++){
    contactNames.push({"text":contactContractorNames[j],'value':contactContractors[j]});
}

$("#participatingContractors").combobox("loadData",contactNames);
var selectActivity=$("#selectActivity").val().split(',');

var selectParticipating=$("#selectParticipating").val().split(',');

$("#participatingContractors").combobox("setValues",selectParticipating);

$("#activityForm").combobox("setValues",selectActivity);

});
var getSelectedInformation = function (data) {
    $("#participatingContractors").combobox("clear");
    $("#majorHazardId").val(data[0].id);
    $("#majorHazardName").textbox("setValue",data[0].devicePartName);
    $("#contactContractorName").textbox("setValue",data[0].contactContractorName);
    $("#level").textbox("setValue",data[0].level);
    $("#unitName").textbox("setValue",data[0].belongsUnitName);
    var ids=data[0].contactContractor.split(",");
    var names=data[0].contactContractorName.split(",");
    var datas=[];
    for(var i=0;i<ids.length;i++){
        datas.push({"text":names[i],"id":ids[i],'value':names[i]});
    }
    $("#participatingContractors").combobox("loadData",datas);

};
$('#majorHazardName').textbox({
    required: true,
    icons: [{
        iconCls: 'icon-search',
        handler: function () {
            WindowManager.openLayer({
                type: 2,
                title: '关键装置/要害部位',
                area: ['90%', '70%'],
                content: ROOT_PATH + '/majorHazard/informationRegistration/simpleList.shtml',
                callBack:getSelectedInformation
            });
        }
    }],
    editable: false
});

function openDetailPage(id){
    WindowManager.openLayer({
        type: 2,
        title: '问题详情',
        area: ['1150px', '70%'],
        content:modelPath+"problemDetail.shtml?id="+id
    });
}

$("#precautionList").datagrid({
    method: 'post',
    url: modelPath + 'problemList.shtml',
    queryParams: {
        activityRecordId:$("#id").val(),
        sort: 'id',
        order: 'desc'
    },
    iconCls: 'icon-save', //图标
    rownumbers:true,
    singleSelect:true,
    fit: true, //datagrid自适应宽度
    fitColumns: true, //列自适应宽度
    striped: true, //行背景交换
    nowap: true, //列内容多时自动折至第二行
    toolbar:'#tb',
    idField: 'id', //主键
    columns: [[//显示的列
        {field:'checkId',width:100,checkbox:true},
        {field:'problemDescription',title:'问题描述',width:200,
            formatter:function(value,row,index){
                return "<a href='javascript:void(0);' style='color: deepskyblue;' onclick='openDetailPage(\""+row.id+"\")'>"+value+"</a>"
            }
        },
        {field:'beCheckedUnit',title:'被检单位(部门)/承包商',width:200,formatter:function(value,row,index){
            if(row.isContractor==1){
                return row.contractorName
            }else {
                return value
            }
        }},
        {field:'specialtyClassificationName',title:'专业分类',width:200},
        {field:'rectificationRequirementsNames',title:'整改要求',width:200},
        {field:'rectificationPeriodNames',title:'整改期限',width:200},
        {field:'rectificationContactNames',title:'整改联络人',width:300},
        {field:'rectState',title:'整改状态',width:300,formatter:function(value,row){
            if(value==0){
                return '<span class="color-success text-white" style="padding: 3px;border-radius:5px;">已验收</span>';
            }else if(value==1){
                return '<span class="color-important text-white" style="padding: 3px;border-radius:5px;">整改中</span>';
            }else {
                return "未下发";
            }
        }}
    ]]
});

//表单提交
$('#btn_save').on('click',function () {
    var index=layer.confirm('是否确定保存？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $('#form1').form('submit', {
            url: modelPath+'edit.shtml',

            onSubmit: function (param) {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    layer.close(index);
                    layer.msg("请正确填写表单")
                }
                return $(this).form('validate');
            },
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success){
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    layer.msg(result.message,{time:1000,icon:1},function () {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        $.getIframeWindow().$('#dg1').datagrid('load');
                    });
                }else {
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:2});
                }
            }
        });
    });

});
$("#btn_precaution_add").click(function(){
    var keyDevice=$("#majorHazardName").textbox("getValue");
    var inspectedUnit=$("#unitName").textbox("getValue");
    var activityRecordId=$("#id").val();
    WindowManager.openLayer({
        type: 2,
        title: '新增问题',
        area: ['1150px', '70%'],
        content:modelPath+"problem.shtml?keyDevice="+keyDevice+"&inspectedUnit="+inspectedUnit+"&activityRecordId="+activityRecordId,
        callBack:function(){
            $('#precautionList').datagrid('load');
        }
    });
});
$("#btn_precaution_edit").click(function(){
    var checkedRows=$('#precautionList').datagrid('getChecked');

    if(checkedRows.length == 0){
        layer.alert("选择需要编辑的行！");
        return
    }else if (checkedRows.length > 1){
        layer.alert("只能选中一行数据！");
        return
    }
    var rectState=checkedRows[0].rectState;
    if(rectState==0||rectState==1){
        layer.alert("已经下发任务不能编辑");
        return
    }
    var id=checkedRows[0].id;
    WindowManager.openLayer({
        type: 2,
        title: '编辑问题',
        area: ['1150px', '70%'],
        content:modelPath+"problemEdit.shtml?id="+id,
        callBack:function(){
            $('#precautionList').datagrid('load');
        }
    });
});
$('#btn_precaution_delete').on('click',function () {

    var checkedRows=$('#precautionList').datagrid('getChecked');

    if(checkedRows.length == 0){
        layer.alert("选择需要删除的行！");
        return
    }else if (checkedRows.length > 1){
        layer.alert("只能选中一行数据！");
        return
    }
    var rectState=checkedRows[0].rectState;
    if(rectState==0||rectState==1){
        layer.alert("已经下发任务不能删除");
        return
    }
    var id=checkedRows[0].id;
    layer.confirm('是否确定删除当前选中数据？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type: "POST",
            url: modelPath+'problemDelete.shtml',
            data: {id:id},
            dataType: "json",
            success: function(result){
                if (result.success) {
                    $('#precautionList').datagrid('clearSelections').datagrid('reload');
                    layer.msg('删除成功', {
                        icon: 1,
                        time: 1000
                    });
                }
                else {
                    layer.msg('删除失败', {
                        icon: 2,
                        time: 1000
                    });
                }
            }
        });
    });

});